<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品搜索页</title>
    <style>
        * {
            list-style: none;
            margin: 0px;
            padding: 0px;
            border:none;outline:none;
        }

        a{
            text-decoration: none;
            color: #000
        }

        .menus {
            display: flex;
            height: 40px;
            align-items: center;
        }

        .menus li {
            border-radius: 3px;
            padding: 5px 20px;
            margin: 0px 20px;
            cursor: pointer;
        }

        .menus li.item:hover {
            background: orangered;
            color: #fff;
        }

        .menus li.item.active {
            background: orangered;
            color: #fff;
        }

        .container {
            width: 800px;
            margin: 30px auto;
            border: 1px solid gainsboro;
        }

        .goodslist {
            display: flex;
            flex-wrap: wrap
        }

        .goodslist .item {
            width: 200px;
            height: 300px;
            padding: 10px;
            font-size: 14px;
            box-sizing: border-box;
        }

        .goodslist .item img {
            width: 100%;
            height: 180px;
        }

        .searcher{
            width: 100%;
            height: 100px;
            display: flex;
            align-items: center;
            justify-content: center;

        }

        .btn{
            color: #fff;
            letter-spacing: 2px;
            width:80px;
            background: orangered;
            border: 1px solid orangered;
        }


        [v-cloak]{
            display: none
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <a href="shopcart.html">购物车</a>
        <div class="container">
            <div class="searcher">
                <div style="display:flex;height:40px;justify-content: center;border:1px solid orangered">
                    <input v-model="keywords" @keydown.enter="getGoodsList" type="text" style="width:300px;text-indent: 10px" />
                    <button class="btn" @click="getGoodsList">搜索</button>
                </div>
            </div>
            <sb-menu title="品牌" :list="brandlist" v-model="currentbrandid"></sb-menu>
            <sb-menu title="适用人群" :list="persontypelist" v-model="currentpersonTypeid"></sb-menu>
            <sb-menu title="风格" :list="stylelist" v-model="currentstyleid"></sb-menu>
            <ul class="goodslist">
                <li class="item" v-for="item in goodslist">
                    <a :href="'detail.html?id='+item.id">
                        <img :src="item.img">
                        <div style="height:40px;overflow:hidden">{{item.title}}</div>
                        <div style="color:red;">￥{{item.price.toFixed(2)}} </div>
                    </a>
                    <div>
                        <button @click="addToCart(item.id)" style="background:orangered;color:#fff;padding:5px 10px;cursor: pointer;">加入购物车</button>
                    </div>
                </li>
            </ul>
        </div>
    </div>

    <script src="https://cdn.bootcss.com/vue/2.6.4/vue.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
    <script src="components/menu.js"></script>
    <script>
        Vue.config.productionTip = false
        Vue.config.devtools = false

        new Vue({
            el: "#app",
            watch: {
                currentbrandid(val) {
                    this.getGoodsList()
                },
                currentpersonTypeid(val) {
                    this.getGoodsList()
                },
                currentstyleid(val) {
                    this.getGoodsList()
                }
            },
            data: {
                keywords: "",
                currentbrandid: 0,
                currentpersonTypeid: 0,
                currentstyleid: 0,
                brandlist: [],
                persontypelist: [],
                stylelist: [],
                goodslist: [],
                shopcartList: []
            },
            created() {
                this.getBrandList()
                this.getpersonTypeList()
                this.getStyleList()
                this.getGoodsList()

                var old = localStorage.getItem("shopcart")
                if (old) {
                    this.shopcartList = JSON.parse(old)
                }
            },
            methods: {
                addToCart(id) {

                    var isalive = false
                    for (let i = 0; i < this.shopcartList.length; i++) {
                        if (this.shopcartList[i].id === id) {
                            this.shopcartList[i].count++
                            isalive = true
                            break
                        }
                    }
                    if (!isalive) {
                        this.shopcartList.push({
                            id: id,
                            count: 1
                        })
                    }

                    localStorage.setItem("shopcart", JSON.stringify(this.shopcartList))
                },
                getBrandList() {
                    axios.get("data/brand.json").then(res => {
                        this.brandlist = res.data
                        //在开头加上全部
                        this.brandlist.unshift({
                            id: 0,
                            name: "全部"
                        })
                    })
                },
                getpersonTypeList() {
                    axios.get("data/personType.json").then(res => {
                        this.persontypelist = res.data
                        //在开头加上全部
                        this.persontypelist.unshift({
                            id: 0,
                            name: "全部"
                        })
                    })
                },
                getStyleList() {
                    axios.get("data/style.json").then(res => {
                        this.stylelist = res.data
                        //在开头加上全部
                        this.stylelist.unshift({
                            id: 0,
                            name: "全部"
                        })
                    })
                },
                getGoodsList() {
                    this.goodslist = []
                    setTimeout(() => {
                        axios.get("data/goods.json").then(res => {

                            var result = res.data

                            if (this.currentbrandid) {
                                result = result.filter(r => r.brandid === this.currentbrandid)
                            }
                            if (this.currentstyleid) {
                                result = result.filter(r => r.styleid === this.currentstyleid)
                            }
                            if (this.currentpersonTypeid) {
                                result = result.filter(r => r.personTypeid === this.currentpersonTypeid)
                            }

                            this.goodslist = result.filter(item => {
                                return item.title.indexOf(this.keywords) > -1
                            })
                        })
                    }, 500)
                }
            },
        })
    </script>
</body>

</html>